package com.ckerchina.admin.ckadmin.service.mgr;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ckerchina.admin.ckadmin.dao.AgentUserDao;
import com.ckerchina.admin.ckadmin.dao.MgrMenuDAO;
import com.ckerchina.admin.ckadmin.dao.MgrUserAndMenuDAO;
import com.ckerchina.admin.ckadmin.dao.MgrUserDAO;
import com.ckerchina.admin.ckadmin.domain.AgentUser;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrMenu;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrUser;
import com.ckerchina.admin.ckadmin.domain.mgr.MgrUserAndMenu;
import com.ckerchina.admin.ckadmin.exception.ErrorCode;
import com.ckerchina.admin.ckadmin.exception.ZhuziException;

@Service
public class MgrUserService {

	@Autowired
	private MgrUserDAO mgrUserDAO;
	@Autowired
	private MgrMenuDAO mgrMenuDAO;
	@Autowired
	private MgrUserAndMenuDAO mgrUserAndMenuDAO;
	
	@Autowired
	private AgentUserDao agentUserDao;

	@Transactional
	public void persistUser(MgrUser user){
		mgrUserDAO.persist(user);
	}
	
	@Transactional
	public MgrUser mergeUser(MgrUser user){
		return mgrUserDAO.merge(user);
	}
	
	@Transactional
	public void persistMenu(MgrMenu menu){
		mgrMenuDAO.persist(menu);
	}
	
	@Transactional
	public MgrMenu mergeMenu(MgrMenu menu){
		return mgrMenuDAO.merge(menu);
	}
	
	@Transactional
	public void delMgrUser(Long id) {
		MgrUser user = mgrUserDAO.find(id);
		if (user != null) {
			mgrUserDAO.remove(user);
		}
		List<MgrUserAndMenu> list = mgrUserAndMenuDAO.findAllByMuserId(id);
		for (MgrUserAndMenu mm : list) {
			mgrUserAndMenuDAO.remove(mm);
		}
	}

	@Transactional
	public void delMgrMenu(Long id) {
		MgrMenu menu = mgrMenuDAO.find(id);
		if (menu != null) {
			mgrMenuDAO.remove(menu);
		}
		List<MgrUserAndMenu> list = mgrUserAndMenuDAO.findAllByMenuId(id);
		for (MgrUserAndMenu mm : list) {
			mgrUserAndMenuDAO.remove(mm);
		}
	}

	@Transactional
	public void updateUserMenu(Long userId, Long menuId, Integer type) {
		MgrUser mgrUser = mgrUserDAO.find(userId);
		if (mgrUser == null) {
			throw new ZhuziException(ErrorCode.Data_Error, "用户不存在");
		}
		MgrUserAndMenu userMenu = mgrUserAndMenuDAO.findByMuserIdAndMenuId(userId, menuId);
		if (userMenu == null) {
			userMenu = new MgrUserAndMenu(userId, menuId, type);
			mgrUserAndMenuDAO.persist(userMenu);
		} else {
			if (userMenu.getState() != type) {
				userMenu.setState(type);
				mgrUserAndMenuDAO.merge(userMenu);
			}
		}
	}
	
	@Transactional
	public void mergeMgrUser(Long id, String name,String realname, String pwd,String email,String actortype,String companyid,Integer state) {
		if (id != null) {
			MgrUser user = mgrUserDAO.find(id);
			user.setName(name);
		    user.setRealname(realname);
			user.setPwd(pwd);
			user.setEmail(email);
			user.setActortype(actortype);
			user.setCompanyid(companyid);
			
			AgentUser agent_user = agentUserDao.find(Long.parseLong(companyid));
			user.setCompanyname(agent_user.getAgentName());
			user.setState(state);
			mgrUserDAO.merge(user);
		} else {
			MgrUser user = new MgrUser();
			user.setName(name);
			user.setRealname(realname);
			user.setPwd(pwd);
			user.setEmail(email);
			user.setActortype(actortype);
			user.setCompanyid(companyid);
			user.setState(1);
			AgentUser agent_user = agentUserDao.find(Long.parseLong(companyid));
			user.setCompanyname(agent_user.getAgentName());
			user.setCreateTime(new Date());
			mgrUserDAO.persist(user);
		}
	}
	
	@Transactional
	public void mergeMgrMemu(Long id, String name, String url, String iconCls) {
		if (id != null) {
			MgrMenu menu = mgrMenuDAO.find(id);
			menu.setName(name);
			menu.setUrl(url);
			menu.setIconCls(iconCls);
			mgrMenuDAO.merge(menu);
		} else {
			MgrMenu menu = new MgrMenu();
			menu.setName(name);
			menu.setUrl(url);
			menu.setIconCls(iconCls);
			menu.setSortNo(mgrMenuDAO.findMaxSortNo() + 1);
			mgrMenuDAO.persist(menu);
		}
	}
}
